package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void addContact(Long userId, Long friendId) {
        //1.我加对方
        if(!isFriend(userId,friendId)){
            addFriendAndMovement(userId,friendId);
        }

        //2.对方加我
        if (!isFriend(friendId,userId)) {
            addFriendAndMovement(friendId,userId);
        }
    }

    @Override
    public PageBeanVo findContactsByPage(Long userId, Integer pageNum, Integer pageSize) {
        //1.创建条件对象(userId,排序,分页)
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2.查询好友列表
        List<Friend> friends = mongoTemplate.find(query, Friend.class);

        //3.查询总记录数
        query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, Friend.class);

        //4.封装pageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,count,friends);
    }

    //添加好友和将我的动态复制给好友的好友动态表中
    private void addFriendAndMovement(Long userId, Long friendId) {
        //1.添加好友
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        friend.setCreated(System.currentTimeMillis());
        mongoTemplate.save(friend);

        //2.添加动态
        //2.1 获取我的所有动态集合
        List<MyMovement> myMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);

        //2.2 将我的动态list遍历,封装好友动态,放入好友的好友动态表中
        if (CollUtil.isNotEmpty(myMovements)) {
            for (MyMovement myMovement : myMovements) {

                FriendMovement friendMovement = new FriendMovement();
                friendMovement.setUserId(userId);
                friendMovement.setPublishId(myMovement.getPublishId());
                friendMovement.setCreated(myMovement.getCreated());

                mongoTemplate.save(friendMovement,ConstantUtil.MOVEMENT_FRIEND+friendId);
            }
        }
    }

    //判断是否是好友
    private boolean isFriend(Long userId, Long friendId) {
        return mongoTemplate.exists(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
    }
}
